/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-19 11:12:43
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-26 11:14:58
 */
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { LazyLoadEvent } from 'primeng/api';
import { AffairRemindServiceProxy, CommonServiceProxy, AffairRemindDto, LabelCollectInt } from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'app-message',
    templateUrl: './message.component.html',
    styleUrls: ['./message.component.less']
})
export class MessageComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    updateFlag = false;
    updateForm = {
        id: undefined,
        roleIds: [],
        isUsed: true,
        isDefault: true
    }
    roleOptions = [];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _affairRemindServiceProxy: AffairRemindServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {

        this._commonServiceProxy.getRoleList(
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.roleOptions = result;
            });
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._affairRemindServiceProxy.getAffairTy(
            undefined,
            undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    update(row) {
        // 修改
        this.updateFlag = true;
        this.updateForm.id = row.id;
        this.updateForm.isUsed = row.isOpen;
        this.updateForm.isDefault = row.isdefault;
        let ids = row.roleList.split(',');
        let names = row.roleListDesc.split(',');
        for (let i = 0; i < ids.length; i++) {
            let value = ids[i];
            let label = names[i];
            let data = new LabelCollectInt();
            data.label = label;
            data.value = parseInt(value);
            this.updateForm.roleIds.push(data);
        }
        
    }
    onHide(event) {
        this.updateForm.id = undefined;
        this.updateForm.isDefault = undefined;
        this.updateForm.isUsed = undefined;
        this.updateForm.roleIds = [];
    }
    save() {
        //修改保存
        let id = '';
        let name = '';
        for (let i = 0; i < this.updateForm.roleIds.length; i++) {
            let item = this.updateForm.roleIds[i];
            if (i == 0 && this.updateForm.roleIds.length - 1 != 0) {
                id = item.value + ',';
                name = item.label + ',';
            } else if (i == 0 && this.updateForm.roleIds.length - 1 == 0) {
                id = item.value;
                name = item.label;
            } else if (i == this.updateForm.roleIds.length - 1) {
                id = id + item.value;
                name = name + item.label;
            } else {
                id = id + item.value + ',';
                name = name + item.label + ',';
            }
        }
        let input = new AffairRemindDto();
        input.id = this.updateForm.id;
        input.isOpen = this.updateForm.isUsed;
        input.isdefault = this.updateForm.isDefault;
        input.roleList = id;
        input.roleListDesc = name;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._affairRemindServiceProxy.createOrUpdateAffair(
                input
            )
                .pipe(finalize(() => { })).subscribe(result => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('操作成功');
                        this.updateFlag = false;
                        this.getTableList();
                        this.updateForm.id = undefined;
                        this.updateForm.isDefault = undefined;
                        this.updateForm.isUsed = undefined;
                        this.updateForm.roleIds = [];
                    } else {
                        this._messageService.error(result.message);
                    }
                })
        }
    }

}
